package com.wm;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @ClassName: TimeTool
 * @Description: 测试程序运行时间工具
 * @Author: WM
 * @Date: 2021-12-30 15:11
 **/
public class TimeTool {
    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public interface Task {
        void execute();
    }

    public static void check(String title, Task task) {
        if (null == task) {
            return;
        }
        title = (title == null) ? "" : ("【" + title + "】");
        LocalDateTime start = LocalDateTime.now();
        System.out.println("开始时间：" + start.format(formatter));
        task.execute();
        LocalDateTime end = LocalDateTime.now();
        System.out.println("结束时间：" + end.format(formatter));
        Duration duration = Duration.between(start, end);
        System.out.println(title + "耗时" + duration.toMillis() + "毫秒");
    }
}
